﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MenuLib;


namespace MenuBuilder
{
    public class MenuBuilder : IMenuBuilder
    {
        private Menu product;

        public MenuBuilder(Menu product)
        {
            Product = product;
        }

        public Menu Product
        {
            get { return product; }
            private set { product = value; }
        }

        public void Add(bool insert)
        {
            while (true)
            {
                try
                {
                    if (!insert)
                        InsertNewItem(Product.Items.Count);
                    else
                        InsertNewItem();

                    break;
                }
                catch (ApplicationException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public void Remove()
        {
            while (true)
            {
                try
                {
                    RemoveItem();
                    break;
                }
                catch (ApplicationException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private void RemoveItem()
        {
            while (true)
            {
                Console.Write("Type name of the item to remove: ");

                string name = Console.ReadLine();

                MenuItem itemToRemove = Product.Items.FirstOrDefault(
                    item => item.Name == name);

                if (itemToRemove == null)
                {
                    Console.WriteLine("No item in menu with name specified");
                    continue;
                }

                Product.Items.Remove(itemToRemove);
                break;
            }
        }

        private void InsertNewItem()
        {
            while (true)
            {
                Console.Write("Enter line number to insert the new item at: ");

                string sIndex = Console.ReadLine();

                int index = -1;

                if (!int.TryParse(sIndex, out index) || index <= 0)
                {
                    Console.WriteLine("Line number must be non zero positive");
                    continue;
                }

                if (index - 1 > Product.Items.Count)
                {
                    Console.WriteLine("Line number too big");
                    continue;
                }

                InsertNewItem(index - 1);
                break;
            }
        }

        private void InsertNewItem(int index)
        {
            string name = null;
            string text = null;

            Console.Write("Type item name: ");
            name = Console.ReadLine();
            Console.Write("Type item description: ");
            text = Console.ReadLine();

            while (true)
            {
                Console.Write(
                    "Enter hot key char position in description(new line if none): ");

                string sHotKeyPos = Console.ReadLine();

                if (sHotKeyPos != string.Empty)
                {
                    int hotKeyPos = -1;

                    if (!int.TryParse(sHotKeyPos, out hotKeyPos))
                    {
                        Console.WriteLine("Enter numeric value for hot key position");
                        continue;
                    }

                    if (hotKeyPos <= 0 || hotKeyPos > text.Length)
                    {
                        Console.WriteLine("Hot key position must be a positive non zero value that fits the length of item description");
                        continue;
                    }

                    if (!char.IsLetter(text[hotKeyPos - 1]))
                    {
                        Console.WriteLine("Hot key can be letter only");
                        continue;
                    }

                    char hotKey = char.ToLower(text[hotKeyPos - 1]);

                    if (!Product.HotKeyAvailable(hotKey))
                    {
                        Console.WriteLine("The hot key '{0}' is already in use", hotKey);
                        continue;
                    }

                    text = text.Insert(hotKeyPos - 1, "_");

                    Product.Items.Insert(
                        index, new MenuHotKeyedItem(name, text, hotKey));
                    break;
                }
                else
                {
                    Product.Items.Insert(index, new MenuItem(name, text));
                    break;
                }
            }
        }
    }
}
